#include "lnkitem.h"
#include <QGraphicsItem>
#include <QFile>
#include <QtGui>
#include <windows.h>
#include <shlobj.h>
#include <iostream>


lnkItem::lnkItem(QFile &file)
{

    item = new QGraphicsItemGroup();
    QFileInfo info(file);
    filePath = info.absoluteFilePath();
    qDebug() << filePath;
    icon = new QIcon(info.absoluteFilePath());
    QPixmap pix = icon->pixmap(QSize(50,50));
    QGraphicsPixmapItem *rIcon = new QGraphicsPixmapItem(pix);

    item->addToGroup(rIcon);
    if (info.isDir())
        processDir();
    if (info.isFile())
        processFile();
    if (info.isSymLink())
        processLnk();

    qDebug() << rPath;
    qDebug() << this->description;
}


void lnkItem::processLnk()
{
    type = this->LNK;
    wchar_t *lpszPath = new wchar_t[1000];
    wchar_t *lpszDescription = new wchar_t[1000];
    HRESULT hres;
    IShellLink *pShLink;
    WIN32_FIND_DATA wfd;

    // Initialize the return parameters to null strings.
    *lpszPath = '\0';
    *lpszDescription = '\0';

    // Call CoCreateInstance to obtain the IShellLink
    // Interface pointer. This call fails if
    // CoInitialize is not called, so it is assumed that
    // CoInitialize has been called.
    hres = CoCreateInstance( CLSID_ShellLink,
                             NULL,
                             CLSCTX_INPROC_SERVER,
                             IID_IShellLink,
                             (LPVOID *)&pShLink );

    if (SUCCEEDED(hres))
    {
        IPersistFile *ppf;

        // The IShellLink Interface supports the IPersistFile
        // interface. Get an interface pointer to it.

        hres = pShLink->QueryInterface(IID_IPersistFile,
                                       (LPVOID *)&ppf );
        if (SUCCEEDED(hres))
        {
            WCHAR wsz[MAX_PATH];

           int wlen =  filePath.toWCharArray(wsz);
           qDebug() << "length " << wlen;
           wsz[wlen]='\0';
            // Convert the given link name string to a wide character string.
            //           MultiByteToWideChar( CP_ACP, 0,
            //                                lpszLinkName,
            //                                -1, wsz, MAX_PATH );
            // Load the file.
            hres = ppf->Load(wsz, STGM_READ );
            if (SUCCEEDED(hres))
            {
                qDebug() << "enter";
                // Resolve the link by calling the Resolve() interface function.
                // This enables us to find the file the link points to even if
                // it has been moved or renamed.
                hres = pShLink->Resolve(0,
                                        SLR_ANY_MATCH | SLR_NO_UI);
                if (SUCCEEDED(hres))
                {
                    // Get the path of the file the link points to.
                    hres = pShLink->GetPath(lpszPath,
                                            MAX_PATH,
                                            &wfd,
                                            SLGP_SHORTPATH );

                    // Only get the description if we successfully got the path
                    // (We can't return immediately because we need to release ppf &
                    //  pShLink.)
                    if(SUCCEEDED(hres))
                    {
                        // Get the description of the link.
                        hres = pShLink->GetDescription(lpszDescription,
                                                       MAX_PATH );
                    }
                }
            }
            ppf->Release();
        }
        pShLink->Release();

        qDebug() << "hello";

        rPath = QString::fromWCharArray(lpszPath);
        description = QString::fromWCharArray(lpszDescription);
    }

    delete [] lpszPath;
    delete [] lpszDescription;

}

void lnkItem::processDir()
{

}

void lnkItem::processFile()
{

}
